package xiangya.bysj.client.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import xiangya.bysj.entity.Patient;
import xiangya.bysj.service.impl.PatientServiceImpl;
import xiangya.bysj.util.JsonUtil;
import xiangya.bysj.util.StringUtil;

@Controller
@RequestMapping(value = "/client/patient", produces = "application/json;charset=UTF-8")
public class PatientClientController {

	@Resource(name = "patientService")
	private PatientServiceImpl patientService;

	@RequestMapping("/query")
	@ResponseBody
	public List<Patient> query(int userId) {
		List<Patient> list = null;
		try {
			list = patientService.findPatientsByUser(userId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	@RequestMapping("/queryByName")
	@ResponseBody
	public List<Patient> query(String name){
		List<Patient> list = null;
		try {
			list = patientService.findPatientByName(name);
			if(list == null){
				list = new ArrayList<Patient>();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public String add(String patient) {
		String result = StringUtil.RES_FAIL;
		try {
			Patient pat = (Patient) JsonUtil.getObject(patient, Patient.class);
			patientService.save(pat);
			result = StringUtil.RES_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
}
